package org.report4service.views;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

import org.report4service.listeners.AlertMessageListener;
import org.report4service.listeners.LogonListner;
import org.report4service.model.Report;
import org.report4service.services.ReportServiceImpl;
import org.report4service.utils.AlertMessage;
import org.report4service.utils.CommandPool;
import org.report4service.utils.TextPool;

/**
 * Form to enter service records eg, hours,return visits, studies etc.
 * 
 * @author Jmpango
 * 
 */
public class EnterHoursForm extends Form implements CommandListener, AlertMessageListener {

    /** Reference to which form to display */
    private Display display;
    /** Reference to the previous Screen/Form */
    private Displayable prvScreen;
    /** Application title */
    private String title;
    /** LogonListner */
    private LogonListner logonListner;
    /** Display alerts */
    private AlertMessage alertMessage;
    private int selectedDay;
    private int selectedMonth;
    private int selectedYear;
    /** Field to enter hours */
    private TextField hoursField;
    /** Field to enter minutes */
    private TextField minutesField;
    /** Field to enter magazines */
    private TextField magazinesField;
    /** Field to enter brochures */
    private TextField brochuresField;
    /** Field to enter books */
    private TextField booksField;
    /** Field to enter return visits */
    private TextField returnVField;
    private String hour = "", minute = "", mag = "", bro = "", bks = "", rvists = "";
    private ReportServiceImpl reportService;
    private int qForm = 1;
    private Report report;

    public EnterHoursForm(Display display, Displayable prvScreen, String title,
            LogonListner logonListner, int selectedDay, int selectedMonth, int selectedYear,
            String hour, String minute, String mag, String bro, String bks, String rvists, ReportServiceImpl reportService, int qForm) {
        super(TextPool.TITLE());
        setDisplay(display);
        setPrvScreen(prvScreen);
        setLogonListner(logonListner);
        setSelectedDay(selectedDay);
        setSelectedMonth(selectedMonth);
        setSelectedYear(selectedYear);
        setHour(hour);
        setMinute(minute);
        setMag(mag);
        setBro(bro);
        setBks(bks);
        setRvists(rvists);
        setReportService(reportService);
        setqForm(qForm);

        createEnterHourForm();

        getDisplay().setCurrent(this);
    }

    /**
     * Sets up form
     */
    private void createEnterHourForm() {
        minutesField = new TextField(TextPool.MINUTES(), getMinute(), 3, TextField.NUMERIC);
        hoursField = new TextField(TextPool.HOURS(), getHour(), 3, TextField.NUMERIC);
        booksField = new TextField(TextPool.BOOKS(), getBks(), 3, TextField.NUMERIC);
        brochuresField = new TextField(TextPool.BROCHURES(), getBro(), 3, TextField.NUMERIC);
        magazinesField = new TextField(TextPool.MAGAZINES(), getMag(), 3, TextField.NUMERIC);
        returnVField = new TextField(TextPool.RETURNVISITS(), getRvists(), 3, TextField.NUMERIC);

        append("Ministy Details (" + getSelectedDay() + "/" + getSelectedMonth() + "/" + getSelectedYear() + ")");

        append(hoursField);
        append(minutesField);
        append(magazinesField);
        append(brochuresField);
        append(booksField);
        append(returnVField);

        addCommand(CommandPool.btnBack);
        addCommand(CommandPool.btnSave);
        setCommandListener(this);
    }

    public void commandAction(Command command, Displayable arg1) {
        if (command == CommandPool.btnBack) {
            cleanUp();
            getDisplay().setCurrent(getPrvScreen());
        }


        if (command == CommandPool.btnSave) {
            if (hoursField.getString().equals("") && minutesField.getString().equals("") && brochuresField.getString().equals("") && booksField.getString().equals("") && returnVField.getString().equals("") && magazinesField.getString().equals("")) {
                getDisplay().setCurrent(prvScreen);

            } else {
                report = new Report();
                int hr = 0, min = 0, mag = 0, bro = 0, book = 0, rv = 0;

                if (!(hoursField.getString().trim().equals(TextPool.EMPTY_STRING()))) {
                    hr = Integer.parseInt(hoursField.getString().trim());
                }

                if (!(minutesField.getString().trim().equals(TextPool.EMPTY_STRING()))) {
                    min = Integer.parseInt(minutesField.getString().trim());
                }

                if (!(magazinesField.getString().trim().equals(TextPool.EMPTY_STRING()))) {
                    mag = Integer.parseInt(magazinesField.getString().trim());
                }

                if (!(brochuresField.getString().trim().equals(TextPool.EMPTY_STRING()))) {
                    bro = Integer.parseInt(brochuresField.getString().trim());
                }

                if (!(booksField.getString().trim().equals(TextPool.EMPTY_STRING()))) {
                    book = Integer.parseInt(booksField.getString().trim());
                }

                if (!(returnVField.getString().trim().equals(TextPool.EMPTY_STRING()))) {
                    rv = Integer.parseInt(returnVField.getString().trim());
                }

                report.setDate(getSelectedMonth() + "-" + getSelectedYear());
                report.setMinutes((hr * 60) + min);
                report.setMagazines(mag);
                report.setBrochures(bro);
                report.setBook(book);
                report.setReturnVisits(rv);

                alertMessage = new AlertMessage(getDisplay(), TextPool.TITLE(), this, this);
                alertMessage.showConfirm(TextPool.SAVE_CONFIRMATION_TEXT());
            }
        }

    }

    public void onAlertMessage(byte msg) {
        if (msg == AlertMessageListener.MSG_OK) {
            if (!(report.getDate().equals(TextPool.EMPTY_STRING()))) {
                reportService.addReport(report, getqForm());
            }
            cleanUp();
            getDisplay().setCurrent(getPrvScreen());
        } else if (msg == AlertMessageListener.MSG_CANCEL) {
            getDisplay().setCurrent(this);
        }
    }

    private void cleanUp() {
        title = TextPool.EMPTY_STRING();
        logonListner = null;
        alertMessage = null;
        hour = TextPool.EMPTY_STRING();
        minute = TextPool.EMPTY_STRING();
        mag = TextPool.EMPTY_STRING();
        bro = TextPool.EMPTY_STRING();
        bks = TextPool.EMPTY_STRING();
        rvists = TextPool.EMPTY_STRING();
        reportService = null;
        report = null;
    }

    /**
     * @return the display
     */
    public Display getDisplay() {
        return display;
    }

    /**
     * @param display
     *            the display to set
     */
    public void setDisplay(Display display) {
        this.display = display;
    }

    /**
     * @return the prvScreen
     */
    public Displayable getPrvScreen() {
        return prvScreen;
    }

    /**
     * @param prvScreen
     *            the prvScreen to set
     */
    public void setPrvScreen(Displayable prvScreen) {
        this.prvScreen = prvScreen;
    }

    /**
     * @return the title
     */
    public String getTitle() {
        return title;
    }

    /**
     * @param title
     *            the title to set
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * @return the logonListner
     */
    public LogonListner getLogonListner() {
        return logonListner;
    }

    /**
     * @param logonListner
     *            the logonListner to set
     */
    public void setLogonListner(LogonListner logonListner) {
        this.logonListner = logonListner;
    }

    /**
     * @return the alertMessage
     */
    public AlertMessage getAlertMessage() {
        return alertMessage;
    }

    /**
     * @param alertMessage
     *            the alertMessage to set
     */
    public void setAlertMessage(AlertMessage alertMessage) {
        this.alertMessage = alertMessage;
    }

    /**
     * @return the selectedDay
     */
    public int getSelectedDay() {
        return selectedDay;
    }

    /**
     * @param selectedDay
     *            the selectedDay to set
     */
    public void setSelectedDay(int selectedDay) {
        this.selectedDay = selectedDay;
    }

    /**
     * @return the selectedMonth
     */
    public int getSelectedMonth() {
        return selectedMonth;
    }

    /**
     * @param selectedMonth
     *            the selectedMonth to set
     */
    public void setSelectedMonth(int selectedMonth) {
        this.selectedMonth = selectedMonth;
    }

    /**
     * @return the selectedYear
     */
    public int getSelectedYear() {
        return selectedYear;
    }

    /**
     * @param selectedYear
     *            the selectedYear to set
     */
    public void setSelectedYear(int selectedYear) {
        this.selectedYear = selectedYear;
    }

    /**
     * @return the hoursField
     */
    public TextField getHoursField() {
        return hoursField;
    }

    /**
     * @param hoursField
     *            the hoursField to set
     */
    public void setHoursField(TextField hoursField) {
        this.hoursField = hoursField;
    }

    /**
     * @return the minutesField
     */
    public TextField getMinutesField() {
        return minutesField;
    }

    /**
     * @param minutesField
     *            the minutesField to set
     */
    public void setMinutesField(TextField minutesField) {
        this.minutesField = minutesField;
    }

    /**
     * @return the magazinesField
     */
    public TextField getMagazinesField() {
        return magazinesField;
    }

    /**
     * @param magazinesField
     *            the magazinesField to set
     */
    public void setMagazinesField(TextField magazinesField) {
        this.magazinesField = magazinesField;
    }

    /**
     * @return the brochuresField
     */
    public TextField getBrochuresField() {
        return brochuresField;
    }

    /**
     * @param brochuresField
     *            the brochuresField to set
     */
    public void setBrochuresField(TextField brochuresField) {
        this.brochuresField = brochuresField;
    }

    /**
     * @return the booksField
     */
    public TextField getBooksField() {
        return booksField;
    }

    /**
     * @param booksField
     *            the booksField to set
     */
    public void setBooksField(TextField booksField) {
        this.booksField = booksField;
    }

    /**
     * @return the returnVField
     */
    public TextField getReturnVField() {
        return returnVField;
    }

    /**
     * @param returnVField
     *            the returnVField to set
     */
    public void setReturnVField(TextField returnVField) {
        this.returnVField = returnVField;
    }

    public String getBks() {
        return bks;
    }

    public void setBks(String bks) {
        this.bks = bks;
    }

    public String getBro() {
        return bro;
    }

    public void setBro(String bro) {
        this.bro = bro;
    }

    public String getHour() {
        return hour;
    }

    public void setHour(String hour) {
        this.hour = hour;
    }

    public String getMag() {
        return mag;
    }

    public void setMag(String mag) {
        this.mag = mag;
    }

    public String getMinute() {
        return minute;
    }

    public void setMinute(String minute) {
        this.minute = minute;
    }

    public String getRvists() {
        return rvists;
    }

    public void setRvists(String rvists) {
        this.rvists = rvists;
    }

    public ReportServiceImpl getReportService() {
        return reportService;
    }

    public void setReportService(ReportServiceImpl reportService) {
        this.reportService = reportService;
    }

    public int getqForm() {
        return qForm;
    }

    public void setqForm(int qForm) {
        this.qForm = qForm;
    }
}
